using System;
using System.Drawing;
using Microsoft.SmallBasic.Library;

namespace SmallBasicFun.Quizes
{
	public class HighLowQuiz : IQuiz
	{
		public  Question Question1Turn
		{
			get { return Quiz.questions[0]; }
		}
		public  Question Question2Turn
		{
			get { return Quiz.questions[1]; }
		}
		public  Question Question3Message
		{
			get { return Quiz.questions[2]; }
		}
		public  Question Question4Turn
		{
			get { return Quiz.questions[3]; }
		}

		public  void StartTest()
		{
			Tortoise.SetSpeed(10);
			Tortoise.SetPenWidth(3);
			GradeResults();
			InitializeScreen();
			DrawSuccess();
			Grade3Message();
			Quiz.DisplayScores(4, 300);
		}

		private  void Grade3Message()
		{
			Question3Message.Result = MessageBox.GetLastDisplayed() == "elcomeway omehay!";
		}

		private  void DrawSuccess()
		{
			Tortoise.SetSpeed(10);
			ProgramWindow.SetBackgroundColor(Colors.Black);
			Tortoise.SetPenColor(Colors.LawnGreen);
			Tortoise.SetPenWidth(2);
			Primitive moves =
				"2=90;3=-90;4=-90;5=135;6=-45;7=90;8=90;9=-135;10=90;11=135;12=-90;13=90;14=90;15=90;16=-90;17=-90;18=-90;19=-90;20=-90;21=90;22=90;23=90;24=-90;25=90;26=90;27=-90;28=-90;29=-90;";
			Primitive lengths =
				"2=70;3=85;4=25;5=43;6=40;7=18;8=26;9=50;10=113;11=25;12=85;13=40;14=30;15=30;16=20;17=20;18=20;19=10;20=20;21=10;22=10;23=20;24=10;25=10;26=30;27=30;28=60;29=183;";
			for (int j = 2; j < 30; j++)
			{
				Turn();
				AntiTurn();
				Tortoise.Turn(moves[j]);
				Tortoise.Move(lengths[j]);
			}
			Question3Message.Run();
		}

		private  void AntiTurn()
		{
			Anti1Turn();
			Anti2Turn();
			Anti4Turn();
		}

		private  void Anti4Turn()
		{
			if (Tortoise.GetY() > 50)
			{
				Tortoise.Turn(177);
			}
		}

		private  void Anti2Turn()
		{
			if (Tortoise.GetX() < Tortoise.GetY())
			{
				Tortoise.Turn(54);
			}
			else
			{
				Tortoise.Turn(-22);
			}
		}

		private  void Anti1Turn()
		{
			if (Tortoise.GetY() == 115)
			{
				Tortoise.Turn(-63);
			}
		}

		private  void Turn()
		{
			Question1Turn.Run();
			Question2Turn.Run();
			Question4Turn.Run();
		}

		private  void InitializeScreen()
		{
			ProgramWindow.SetBackgroundColor(Colors.Black);
			Tortoise.SetPosition(20,200);
		    Tortoise.SetAngle(0);
		}

		private  void GradeResults()
		{
			GradeTurn(Question1Turn, Anti1Turn, new Point(0, 100), new Point(0, 115), new Point(0, 120));
			GradeTurn(Question2Turn, Anti2Turn, new Point(0, 1), new Point(1, 0), new Point(1, 1));
			GradeTurn(Question4Turn, Anti4Turn, new Point(0, 25), new Point(0, 50), new Point(0, 51));
		
		}

		private  void GradeTurn(Question question, Action inverse, params Point[] points)
		{
			Tortoise.SetAngle(0);
			foreach (var p in points)
			{
				Tortoise.SetPosition(p.X,p.Y);
				inverse();
				question.Run();
				if (Tortoise.GetAngle() !=0)
				{
					question.Result = false;
					return;
				}

			}
			question.Result = true;
		}

	}
}